﻿using Deployment.Data.Environment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Environment
{
    /// <summary>
    /// Interaction logic for EnvironmentsEditorControl.xaml
    /// </summary>
    public partial class EnvironmentsEditorControl : UserControl
    {
        public static readonly DependencyProperty EnvironmentsFilePathProperty =
            DependencyProperty.Register("EnvironmentsFilePathText", typeof(string),
            typeof(EnvironmentsEditorControl), null);

        public static readonly DependencyProperty HasUnsavedChangesProperty =
            DependencyProperty.Register("HasUnsavedChanges", typeof(bool),
            typeof(EnvironmentsEditorControl), null);

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        public string EnvironmentsFilePath
        {
            get { return (string)GetValue(EnvironmentsFilePathProperty); }
            set { SetValueDp(EnvironmentsFilePathProperty, value); }
        }

        public bool HasUnsavedChanges
        {
            get { return (bool)GetValue(HasUnsavedChangesProperty); }
            set { SetValueDp(HasUnsavedChangesProperty, value); }
        }

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public EnvironmentsEditorControl()
        {
            InitializeComponent();
            this.DataContext = new EnvironmentsFile();
        }

        //public EnvironmentsEditorControl(EnvironmentsFile data)
        //{
        //    InitializeComponent();
        //    if (data == null)
        //        data = new EnvironmentsFile();
        //    this.DataContext = data;
        //}

        public bool SaveChanges()
        {
            string filePath = EnvironmentsFilePath;
            if (string.IsNullOrEmpty(filePath))
            {
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Connections.xml";
                dlg.DefaultExt = ".xml";
                dlg.Filter = "XML Document |*.xml|All Files|*.*";

                Nullable<bool> result = dlg.ShowDialog(Window.GetWindow(this));
                if (result == true)
                {
                    EnvironmentsFilePath = dlg.FileName;
                }
            }

            if (!string.IsNullOrEmpty(EnvironmentsFilePath))
            {
                EnvironmentsFile file = DataContext as EnvironmentsFile;
                if (file != null)
                {
                    file.SaveToFile(EnvironmentsFilePath);
                }
                HasUnsavedChanges = false;
                return true;
            }
            else
            {
                MessageBox.Show(Window.GetWindow(this), "Save has been cancelled", "Operation Cancelled", MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveChanges();
        }
    }
}
